package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClazzService clazzService;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> queryStudentPage(StudentQueryParam param) {
        Page<Student> page = Page.of(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));

        String name = param.getName();
        Integer degree = param.getDegree();
        Integer clazzId = param.getClazzId();

        Page<Student> p = lambdaQuery().like(name != null && !name.isEmpty(), Student::getName, name)
                .eq(degree != null, Student::getDegree, degree)
                .eq(clazzId != null, Student::getClazzId, clazzId)
                .page(page);

        List<Student> stus = p.getRecords();
        if (!stus.isEmpty()) {
            List<Clazz> clazzs = clazzService.list();
            Map<Long, String> idToClazz = clazzs.stream().collect(Collectors.toMap(Clazz::getId, Clazz::getName));
            stus.forEach(stu -> {
                if (stu.getClazzId() != null) {
                    stu.setClazzName(idToClazz.get(stu.getClazzId()));
                }
            });
        }

        return new PageResult<Student>(p.getTotal(), stus);

    }

    @Override
    public void saveStu(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        save(student);
    }

    @Override
    public void updateStu(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        updateById(student);
    }

    @Override
    public void executeViolation(Integer id, Integer score) {
        // 处理逻辑写在Service层
        Student stu = getById(id);
        stu.setUpdateTime(LocalDateTime.now());
        stu.setViolationCount((short) (stu.getViolationCount() + 1));
        stu.setViolationScore((short) (stu.getViolationScore() + score));
        updateById(stu);
        // 利用lambdaWrapper定义where语句,将条件封装到wrapper中,和处理信息一起传到mapper层处理
        LambdaUpdateWrapper<Student> wrapper = new LambdaUpdateWrapper<Student>().eq(Student::getId, id);
        studentMapper.updateStuVioliation(wrapper, score);
    }
}
